package com.jack.payment.controller.admin.auth;

import com.alibaba.fastjson.JSONObject;
import com.jack.payment.annotation.PagingLimit;
import com.jack.payment.constant.ResultHintConstant;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.common.CommonAskParam;
import com.jack.payment.entity.common.CommonResult;
import com.jack.payment.entity.filter.PayFilter;
import com.jack.payment.entity.filter.RestFilter;
import com.jack.payment.entity.generate.payment.PaymentAttachModel;
import com.jack.payment.entity.generate.user.UserModel;
import com.jack.payment.entity.request.pay.ExecuteCallBackModel;
import com.jack.payment.entity.result.PageVo;
import com.jack.payment.rabbitmq.entity.QueueContentModel;
import com.jack.payment.rabbitmq.produer.MQSender;
import com.jack.payment.service.basic.PayAttachServiceImpl;
import com.jack.payment.service.mongo.QueueLogServiceImpl;
import com.jack.payment.util.common.PageResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Tag(name = "支付数据查看 与操作")
@RequestMapping(value = "/api/v1/paymentAttach")
public class PaymentAttachController {
    private Logger log = LoggerFactory.getLogger(PaymentAttachController.class);

    @Autowired
    private PayAttachServiceImpl payAttachService;
    @Autowired
    private QueueLogServiceImpl queueLogService;
    @Autowired
    private PageResultUtil pageResultUtil;
    @Autowired
    private MQSender mqSender;


    @Operation(summary = "获取调用支付的数据列表")
    @RequestMapping(value = "getCallPaymentList", method = {RequestMethod.POST})
    @ResponseBody
    @PagingLimit
    public CommonResult getCallPaymentList(@RequestBody CommonAskParam<PayFilter.PayAttachFilter> data) throws BusinessException {
        UserModel getUserModel = (UserModel) data.getCommon().getUserId();
        PayFilter.PayAttachFilter requestParam = data.getData();
        requestParam.setUserId(getUserModel.getId());

        Integer resultCount = payAttachService.getFilterPayAttachCount(requestParam);
        // 判断数据是否超出
        pageResultUtil.judgeRequestCount(
                requestParam.getPage(),
                resultCount
        );
        List<PaymentAttachModel> pageList = payAttachService.getFilterPayAttachList(requestParam);
        return CommonResult.ok(new PageVo<PaymentAttachModel>(
                pageList,
                pageResultUtil.resultPageComputer(requestParam.getCount(), resultCount),
                requestParam.getPage() / requestParam.getCount()
        ));
    }


    @Operation(summary = "消息队列执行列表")
    @RequestMapping(value = "getPaymentCallList", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult getPaymentCallList(@RequestBody CommonAskParam<RestFilter.QueueFilter> data) throws BusinessException {
        UserModel getUser = (UserModel) data.getCommon().getUserId();
        RestFilter.QueueFilter filterModel = data.getData();

        Integer count = queueLogService.filterMQLogRecordCount(filterModel);
        // 判断数据是否超出
        pageResultUtil.judgeRequestCount(
                filterModel.getPage(),
                count
        );
        List<QueueContentModel> pageList = queueLogService.filterMQLogRecordList(filterModel);
        return CommonResult.ok(new PageVo<QueueContentModel>(
                pageList,
                pageResultUtil.resultPageComputer(filterModel.getCount(), count), filterModel.getPage()
        ));
    }


    @Operation(summary = "回调重新执行")
    @RequestMapping(value = "executePaymentCallBack", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult executePaymentCallBack(@RequestBody CommonAskParam<ExecuteCallBackModel> data)
            throws BusinessException {
        UserModel getUserModel = (UserModel) data.getCommon().getUserId();
        ExecuteCallBackModel executeCallBackModel = data.getData();
        QueueContentModel queueContentModel =
                queueLogService.getAppointIdQueueContent(executeCallBackModel.getMqId(), executeCallBackModel.getDbName());
        if (queueContentModel != null) {
            queueContentModel.setIsManual(1);
            try {
                Class<?> clazz = Class.forName(queueContentModel.getAttachClass());
                queueContentModel.setData(JSONObject.parseObject(queueContentModel.getData().toString(), clazz));
                mqSender.sendMQMessage(
                        queueContentModel.getExchange(),
                        queueContentModel.getQueueName(),
                        queueContentModel
                );
            } catch (Exception e) {
                log.warn("类型转换错误, 具体错误如下" + e);
                throw new BusinessException(ResultHintConstant.SYSTEM_CODE);
            }
        }
        return CommonResult.ok();
    }
}
